package com.atguigu.shangrongbao.core.controller.admin;


import com.atguigu.common.exception.BusinessException;
import com.atguigu.common.result.R;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.shangrongbao.core.pojo.entity.IntegralGrade;
import com.atguigu.shangrongbao.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Api(tags = "积分等级管理")
@RestController
@RequestMapping("/admin/core/integralGrade")
public class AdminIntegralGradeController {

    @Resource
    private IntegralGradeService integralGradeService;

    /**
     * 查询积分列表
     * @return
     */
    @ApiOperation("积分列表")
    @GetMapping("/list")
    public R list(){
        List<IntegralGrade> list = integralGradeService.list();
        log.info("获取积分列表：{}", list);
        return R.ok().put("data", list);
    }



    @ApiOperation("根据id查询积分信息")
    @GetMapping("/get/{id}")
    public R getIntegralGradeInfo(@PathVariable("id") Long id){
        log.info("积分id:{}", id);
        IntegralGrade integralGrade = integralGradeService.getById(id);
        log.info("获取到的积分信息：{}", integralGrade);
        if(integralGrade != null){
            return R.ok().put("data", integralGrade).message("获取数据成功");
        }else{
            return R.error().message("获取积分数据失败");
        }
    }
    @ApiOperation("保存积分信息")
    @PostMapping("/save")
    public R save(@RequestBody IntegralGrade integralGrade){
        /**
         * 在保存之前校验数据
         */
        boolean result = integralGradeService.save(integralGrade);
        if(result){
            return R.ok().message("保存成功");
        }else{
            return R.error().message("保存失败");
        }
    }
    @ApiOperation("更新积分信息")
    @PostMapping("/update")
    public R update(@RequestBody IntegralGrade integralGrade){
        /**
         * 更新数据之前校验数据
         */
        boolean res = integralGradeService.updateById(integralGrade);
        if(res){
            return R.ok().message("更新成功");
        }
        return R.error().message("更新失败");

    }
    @PutMapping("/update")
    public R updateByInfo(@RequestBody IntegralGrade integralGrade){
        /**
         * 更新数据之前校验数据
         */
        Long id = integralGrade.getId();
        if(id == null){
            throw new BusinessException(ResponseEnum.BODY_MISS_ERROR);
        }
        boolean update = integralGradeService.updateById(integralGrade);
        if(update) {
            return R.ok();
        }
        return R.error();
    }
    @DeleteMapping("/remove/{id}")
    public R remove(@PathVariable("id") Long id){
        /**
         * 删除时会不会报错
          */
        log.info("id=={}", id);
        boolean res = integralGradeService.removeById(id);
        log.info("删除标志：{}", res);

       if(res){
           return R.ok();
       }
       return R.error(ResponseEnum.BAD_SQL_GRAMMAR_ERROR);
    }

}
